package m4;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.ConnectionFactory;

import java.util.Scanner;

public class Producer {
    public static void main(String[] args) throws Exception{
        //连接
        ConnectionFactory f = new ConnectionFactory();
        f.setHost("192.168.64.140");
//        f.setPort(5672);    //默认端口可以省略
        f.setUsername("admin");
        f.setPassword("admin");
        Channel c = f.newConnection().createChannel();

        //定义Direct交换机  direct_logs
        c.exchangeDeclare("direct_logs", BuiltinExchangeType.DIRECT);

        //向交换机发消息,消息携带路由键
        while (true){
            System.out.println("输入消息:");
            String s = new Scanner(System.in).nextLine();
            System.out.println("输入路由键:");
            String key = new Scanner(System.in).nextLine();
            /*
                参数:
                    1.交换机名字
                    2.路由键关键词,通过这个关键词来确定,消息会被发送到哪个队列
                    3.消息的其他属性设置
                    4.消息数据byte[]数组
             */
            c.basicPublish("direct_logs", key, null, s.getBytes());
        }
    }


}
